package com.yanqu.road.dao.impl.activity.operamask;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.operamask.OperaMaskGameData;
import com.yanqu.road.entity.activity.operamask.OperaMaskSceneData;
import com.yanqu.road.entity.activity.operamask.OperaMaskUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class OperaMaskUserDataDaoImpl extends TempDataObjectDao<OperaMaskUserData> {

    public static String getOperaMaskUserDataSql = "select * from `t_u_opera_mask_user_data` where `activityId`=? and `userId`=?";

    public OperaMaskUserData getOperaMaskUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getOperaMaskUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateOperaMaskUserDataSql = "update `t_u_opera_mask_user_data` set singLevel=?,energy=?,recoveryTime=?,gameData=?,sceneDataMap=?,skillMaxLevelMap=?,sumEnergy=?,tokenList=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(OperaMaskUserData operaMaskUserData) {
        operaMaskUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, operaMaskUserData.getSingLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, operaMaskUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, operaMaskUserData.getRecoveryTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, operaMaskUserData.getGameData() == null ? "" : JSON.toJSONString(operaMaskUserData.getGameData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(operaMaskUserData.getSceneDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(operaMaskUserData.getSkillMaxLevelMap())));
        para.put(i++, new DbParameter(Types.INTEGER, operaMaskUserData.getSumEnergy()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(new ArrayList<>(operaMaskUserData.getTokenList()))));
        para.put(i++, new DbParameter(Types.INTEGER, operaMaskUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, operaMaskUserData.getUserId()));
        if (getDb().executeNoneQuery(updateOperaMaskUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertOperaMaskUserDataSql = "insert into `t_u_opera_mask_user_data`(activityId,userId,singLevel,energy,recoveryTime,gameData,sceneDataMap,skillMaxLevelMap,sumEnergy,tokenList) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(OperaMaskUserData operaMaskUserData) {
        operaMaskUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, operaMaskUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, operaMaskUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, operaMaskUserData.getSingLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, operaMaskUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, operaMaskUserData.getRecoveryTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, operaMaskUserData.getGameData() == null ? "" : JSON.toJSONString(operaMaskUserData.getGameData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(operaMaskUserData.getSceneDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(operaMaskUserData.getSkillMaxLevelMap())));
        para.put(i++, new DbParameter(Types.INTEGER, operaMaskUserData.getSumEnergy()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(new ArrayList<>(operaMaskUserData.getTokenList()))));
        if (getDb().executeNoneQuery(insertOperaMaskUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected OperaMaskUserData parseData(ResultSet rs) throws SQLException {
        OperaMaskUserData operaMaskUserData = new OperaMaskUserData();
        operaMaskUserData.setActivityId(rs.getInt("activityId"));
        operaMaskUserData.setUserId(rs.getLong("userId"));
        operaMaskUserData.setSingLevel(rs.getInt("singLevel"));
        operaMaskUserData.setEnergy(rs.getInt("energy"));
        operaMaskUserData.setRecoveryTime(rs.getLong("recoveryTime"));
        String gameDataStr = rs.getString("gameData");
        operaMaskUserData.setGameData(StringUtils.isNullOrEmpty(gameDataStr) ? null : JSON.parseObject(gameDataStr, OperaMaskGameData.class));
        operaMaskUserData.setSceneDataMap(JSON.parseObject(rs.getString("sceneDataMap"), new TypeReference<ConcurrentHashMap<Integer, OperaMaskSceneData>>() {
        }));
        operaMaskUserData.setSkillMaxLevelMap(JSON.parseObject(rs.getString("skillMaxLevelMap"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {
        }));
        operaMaskUserData.setSumEnergy(rs.getInt("sumEnergy"));
        operaMaskUserData.setTokenList(JSON.parseObject(rs.getString("tokenList"), new TypeReference<List<Integer>>() {
        }));
        return operaMaskUserData;
    }

    // ====================================

    public static String getOperaMaskUserDataListSql = "select * from `t_u_opera_mask_user_data` where `activityId`=?";

    public List<OperaMaskUserData> getOperaMaskUserDataList(int activityId) {
        List<OperaMaskUserData> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getOperaMaskUserDataListSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

}
